$(function() {
	$('#imageFile label').eq(0).css({
		"display":"block"
	});
	
	
	
	$('.newbox-btn').click(function(){
		var new1 = $('#imageFile').children().eq(0).children('span').children().attr('src');
		var new2 = $('#imageFile').children().eq(1).children('span').children().attr('src');
		var new3 = $('#imageFile').children().eq(2).children('span').children().attr('src');
		var new4 = $('#imageFile').children().eq(3).children('span').children().attr('src');
		var new5 = $('#imageFile').children().eq(4).children('span').children().attr('src');
		var newArr = [new1,new2,new3,new4,new5];
		if(!newArr[0]){
			mui.toast('请添加图片！');
			return false;
		}
		console.log(newArr);
		var newImgNum = $('#imageFile').attr('data-goodsDatailsimgs');
		console.log(newImgNum);
		var nameOne = $('#name-one').val();
		console.log(nameOne);
		var nameTwo = $('#name-two').val();
		console.log(nameTwo);
		var nameThree = $('#name-three').val();
		console.log(nameThree);
		var nameFour = $('#name-four').val();
		console.log(nameFour);
		var nameFive = $('#name-five').val();
		console.log(nameFive );
		
		var option=$('#newCategory option:selected');
		var cate = option.attr('data-id');
		console.log(cate);
		newboxPic({
			img:newArr,
			goodsName:nameOne,
			goodsPrice:nameTwo,
			goodsQuantity:nameThree,
			goodsColour:nameFour,
			goodsSize:nameFive,
			goodsType:cate,
			goodsDatailsimgs:newImgNum,
			token:getCookieValue("token"),
			goodsId:CD.geturl().id
		},function(data){
			mui.toast('添加成功！');
		});
	});
	
	
	//上传封面图
	var imageInputId = "image_file";
	var uploadImagePath = "https://wmdx.vimi66.com/img-video/photo/uploadImages";
	initImageInput(imageInputId, uploadImagePath);
	
	$('#newCategory').on('tap',function(){
		
	});
	
	
	storeCategory(function(data){
		$('#newCategory').html(template('newProductCategoryTemplate',data))
	});
	
	dotoTemplate({
		goodsId: CD.geturl().id,
		userUuid:CD.geturl().business
	},function(data){
		var goodsType = data.bayId.goodsType;
		console.log(goodsType,123456)
		$('#alls').html(template('allstemplate',data));
	
	if($('#imageFile').children().eq(0).children('span').children().attr('src')!=''){
			$('#imageFile').children().eq(0).children('div').css({"display":"block"})};
			if($('#imageFile').children().eq(1).children('span').children().attr('src')!=''){
			$('#imageFile').children().eq(1).children('div').css({"display":"block"})};
			if($('#imageFile').children().eq(2).children('span').children().attr('src')!=''){
			$('#imageFile').children().eq(2).children('div').css({"display":"block"})};
			if($('#imageFile').children().eq(3).children('span').children().attr('src')!=''){
			$('#imageFile').children().eq(3).children('div').css({"display":"block"})};
			if($('#imageFile').children().eq(4).children('span').children().attr('src')!=''){
			$('#imageFile').children().eq(4).children('div').css({"display":"block"})};
	
	
	$('#imageFile').children().eq(0).children('div').click(function(){
			window.urlall.splice(0,1);
			$(this).parent().remove();
		});
		$('#imageFile').children().eq(1).children('div').click(function(){
			window.urlall.splice(1,1);
			$(this).parent().remove();
		});
		$('#imageFile').children().eq(2).children('div').click(function(){
			window.urlall.splice(2,1);
			$(this).parent().remove();
		});
		$('#imageFile').children().eq(3).children('div').click(function(){
			window.urlall.splice(3,1);
			$(this).parent().remove();
		});
		$('#imageFile').children().eq(0).children('div').click(function(){
			window.urlall.splice(4,1);
		});
	
		
		chooseOfCategory({
			id:goodsType
		},function(data){
			var newgoodsType = data.typeName;
			$('#newCategory option:selected').text(newgoodsType);
			$('#newCategory option:selected').attr('data-id',goodsType);
			$('#newCategory').on('tap',function(){
				storeCategory(function(data){
				$('#newCategory').html(template('newProductCategoryTemplate',data))
				});	
				return false;
			});			
		});		
	});
	
	
	
});

//分类选择的值
var chooseOfCategory = function(params,callback){
	$.ajax({
		async : false,
        cache : false,
        traditional: true,
		type: "get",		
		url: window.address + "/TbType/findBayId",
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		data:params,
		dataType: "json",
		success: function(data) {
			console.log(data, 33333)
			callback && callback(data);
		}
	});
}



//修改信息遍历
var dotoTemplate = function(params,callback) {
	$.ajax({
		type: "get",
		url: window.address + "/goods/findBayId",
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		data: params,
		dataType: "json",
		success: function(data) {
			console.log(data, 999);
			callback && callback(data);
		}
	});
};


//传值
var newboxPic = function(params,callback){
	$.ajax({
		async : false,
        cache : false,
        traditional: true,
		type: "get",		
		url: window.address + "/goods/saveBayUuid",
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		data:params,
		dataType: "json",
		success: function(data) {
			console.log(data, 22222)
			callback && callback(data);
		}
	});
};

//卖家分类
var storeCategory = function(callback) {
	$.ajax({
		type: 'get',
		url: window.address + '/goods/findBayUuidAndTypes',
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		data: {
			"userUuid":CD.geturl().business
		},
		dataType: 'json',
		success: function(data) {
			console.log(data,888);
			callback && callback(data);
		}
	});
}







window.urlall = [];
var v_img_url = '';
var lengthlll;

function initImageInput(inputId, uploadUrl) {
	var control = $("#" + inputId);
	//alert(control);
	control.fileinput({
		language: "zh", //设置语言
		theme: "fa",
		uploadUrl: uploadUrl, //上传地址
		uploadAsync: true, //是否异步上传   默认异步上传
		uploadExtraData: function(previewId, index) { //上传文件时额外传递的参数设置 动态获取
			var filePathString = $("#filePathString").val();
			if(filePathString == null || filePathString == undefined) filePathString = "";
			var obj = {
				"filePathString": filePathString
			};
			return obj;
		},
		showUpload: false, //是否显示上传按钮
		showRemove: true, //是否显示移除按钮
		showPreview: true, //是否显示预览
		showCaption: true, //是否显示文件标题。默认为true。
		//browseClass:"btn btn-primary", //按钮样式
		dropZoneEnabled: false, //是否显示拖拽区域
		showClose: false, //是否在预览中显示关闭图标
		showUploadedThumbs: false, // 是否在预览窗口中持续显示上传的文件缩略图（用于ajax上传）
		//showBrowse: true, //是否显示文件浏览按钮

		//enctype: 'multipart/form-data',
		rtl: false, //预览显示从右往左  选择文件按钮 从左到右

		layoutTemplates: {
			actionDelete: "", //去除缩略图上面的删除按钮
			actionUpload: "", //去除缩略图上面的上传按钮
			//actionZoom:"",//去除缩略图上面的查看详情按钮
		},
		autoReplace: true, //是否自动替换当前图片，设置为true时，再次选择文件， 会将当前的文件替换掉。
		//minImageWidth: 50, //图片的最小宽度
		//minImageHeight: 50,//图片的最小高度
		//maxImageWidth: 1000,//图片的最大宽度
		//maxImageHeight: 1000,//图片的最大高度
		//minFileCount: 0, //表示同时最小上传的文件个数
		maxFileCount: 1, //表示允许同时上传的最大文件个数
		validateInitialCount: true,

		allowedPreviewTypes: ["image"], //预览文件类型
		allowedFileTypes: ["image"],
		allowedFileExtensions: ["jpg", "png"],
		maxFileSize: 2048, //单位为kb，如果为0表示不限制文件大小

		overwriteInitial: true,
		initialPreviewAsData: true, // identify if you are sending preview data only and not the raw markup
		initialPreviewFileType: "image",
		initialPreview: v_img_url

	}).on("filebatchselected", function(event, files) {
		$(this).fileinput("upload");
	}).on("filepreupload", function(event, data, previewId, index) { //上传中
		var form = data.form,
			files = data.files,
			extra = data.extra,
			response = data.response,
			reader = data.reader;
		console.log("图片正在上传");
	}).on("fileuploaded", function(event, data, previewId, index) { //一个文件上传成功
		console.log('文件上传成功！' + data);
		//获取后台返回的json对象
		var response_result = data.response;
		//获取json对象中的Object值
		var v_fileSrc = response_result.success;
//		console.log(v_fileSrc, 99999)

		window.urlall.push(v_fileSrc);
		console.log(window.urlall,222)
		lengthlll = window.urlall.length;
		console.log(lengthlll,111)
		
		var newBox = $('<label><span><img src="" id="img-url-one"/></span><div id="newbox-x-1"><img src="img/newbox-x.png" /></div></label>')
		$('#imageFile').prepend(newBox);
		var imgUrl1 = window.urlall[0];
		var imgUrl2 = window.urlall[1];
		var imgUrl3 = window.urlall[2];
		var imgUrl4 = window.urlall[3];
		var imgUrl5 = window.urlall[4];
				
		$('#imageFile').children().eq(0).children('span').children().attr('src',imgUrl1);
		$('#imageFile').children().eq(1).children('span').children().attr('src',imgUrl2);
		$('#imageFile').children().eq(2).children('span').children().attr('src',imgUrl3);
		$('#imageFile').children().eq(3).children('span').children().attr('src',imgUrl4);
		$('#imageFile').children().eq(4).children('span').children().attr('src',imgUrl5);
		
			if($('#imageFile').children().eq(0).children('span').children().attr('src')!=''){
			$('#imageFile').children().eq(0).children('div').css({"display":"block"})};
			if($('#imageFile').children().eq(1).children('span').children().attr('src')!=''){
			$('#imageFile').children().eq(1).children('div').css({"display":"block"})};
			if($('#imageFile').children().eq(2).children('span').children().attr('src')!=''){
			$('#imageFile').children().eq(2).children('div').css({"display":"block"})};
			if($('#imageFile').children().eq(3).children('span').children().attr('src')!=''){
			$('#imageFile').children().eq(3).children('div').css({"display":"block"})};
			if($('#imageFile').children().eq(4).children('span').children().attr('src')!=''){
			$('#imageFile').children().eq(4).children('div').css({"display":"block"})};
		
		$('#imageFile').children().eq(0).children('div').click(function(){
			window.urlall.splice(0,1);
			$(this).parent().remove();
		});
		$('#imageFile').children().eq(1).children('div').click(function(){
			window.urlall.splice(1,1);
			$(this).parent().remove();
		});
		$('#imageFile').children().eq(2).children('div').click(function(){
			window.urlall.splice(2,1);
			$(this).parent().remove();
		});
		$('#imageFile').children().eq(3).children('div').click(function(){
			window.urlall.splice(3,1);
			$(this).parent().remove();
		});
		$('#imageFile').children().eq(0).children('div').click(function(){
			window.urlall.splice(4,1);
		});
		






		$("#img_src").val(v_fileSrc);
//		console.log($("#img_src").val(v_fileSrc), 888888)
	}).on("fileerror", function(event, data, msg) { //一个文件上传失败
		console.log("图片上传失败！" + data.id);
		layer.alert(msg, {
			icon: 2
		});
	}).on("filecleared", function(event) { //点击移除按钮 清除预览中的文件后触发的事件
		console.log("删除预览图片！" + event);
		$("#img_src").val(""); //清除预览中的文件后同时将图片地址置为空
	});

}



